package com.fwixvye.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.fwixvye.pojo.*;
import com.fwixvye.pojo.Character;
import com.fwixvye.pojo.rolePack.R040班尼特;
import com.fwixvye.service.CharacterDamageCalculationDetailedService;
import com.fwixvye.utils.CalculationUtil;
import com.fwixvye.utils.OcrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

@Service
public class CharacterDamageCalculationDetailedServiceImpl implements CharacterDamageCalculationDetailedService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 截图转换成词条
     *
     * @param file      前端传来的图片文件
     * @param character 前端传来的character
     * @param weapon    前端传来的weapon
     * @param relics1   前端传来的relics1
     * @param relics2   前端传来的relics2
     * @return 统一返回类型
     */
    @Override
    public Result ScreenshotOfEntry(MultipartFile file, Character character, Weapon weapon, Relics relics1, Relics relics2, Role role) throws IOException {
        OcrUtil ocrUtil = new OcrUtil();
        OcrResponseBody ocrResponseBody = ocrUtil.ImageToOcrResponseBody(file, restTemplate);
        if (ocrResponseBody == null) {
            return Result.err("图片无有效信息");
        }

        // list装的是图片通过OCR转成的文字，有不需要的信息
        ArrayList<DataItem> list = new ArrayList<>(ocrResponseBody.getData());


        // 将行高不超过10的字符串放入到同一个HashMap中
        HashMap<String, String> hashMap = new LinkedHashMap<>();
        for (int i = 0; i < list.size() - 1; i++) {
            DataItem dataItem0 = list.get(i);
            DataItem dataItem1 = list.get(i + 1);
            if ((dataItem1.getBox().get(0).get(1) - dataItem0.getBox().get(0).get(1)) < 10 &&
                    (dataItem1.getBox().get(2).get(1) - dataItem0.getBox().get(2).get(1)) < 10) {
                hashMap.put(dataItem0.getText(), dataItem1.getText());
            }
        }
        /*
            {生命值上限=12,071 +7,664, 攻击力=646, 646=+533, 防御力=799, 799=+852, ?=元素精通, 元素精通=16, 体力上限=240, 暴击率=70.3%, 148.7%=暴击伤害, 治疗加成=0.0%, 受治疗加成=0.0%, 元素充能效率=177.4%, 冷却缩减=0.0%, 0.0%=UID:187215260, 护盾强效=0.0%, 火元素伤害加成=0.0%, 水元素伤害加成=0.0%}
            {火元素伤害加成=0.0%, 火元素抗性=15.0%, 水元素抗性=15.0%, 草元素抗性=15.0%, 雷元素伤害加成=0.0%, 雷元素抗性=15.0%, 风元素伤害加成=0.0%, 风元素抗性=15.0%, 冰元素伤害加成=0.0%, 冰元素抗性=15.0%, 岩元素伤害加成=0.0%, 岩元素抗性=15.0%, 物理伤害加成=0.0%, 物理抗性=15.0%}
        */
//        System.out.println(hashMap);

        Set<String> keySet = hashMap.keySet();
        for (String key : keySet) {
            try {
                if (key.contains("生命值上限")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.hp_p = 0;
                    if (value.contains("+")) {
                        role.hp_r = Double.parseDouble(value.split("\\+")[1]);
                    } else {
                        role.hp_r = Double.parseDouble(hashMap.get(hashMap.get(key)).split("\\+")[1].replace(",", ""));
                    }
                }
                if (key.contains("攻击力")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.atk_p = 0;
                    if (value.contains("+")) {
                        role.atk_r = Double.parseDouble(value.split("\\+")[1]);
                    } else {
                        role.atk_r = Double.parseDouble(hashMap.get(hashMap.get(key)).split("\\+")[1].replace(",", ""));
                    }
                }
                if (key.contains("防御力")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.def_p = 0;
                    if (value.contains("+")) {
                        role.def_r = Double.parseDouble(value.split("\\+")[1]);
                    } else {
                        role.def_r = Double.parseDouble(hashMap.get(hashMap.get(key)).split("\\+")[1].replace(",", ""));
                    }
                }
                if (key.contains("元素精通")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.em_r = Double.parseDouble(value);
                }
                if (key.contains("体力上限")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.ms_r = Double.parseDouble(value);
                }
                if (key.contains("暴击率")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.crr_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("暴击伤害")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.crd_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("治疗加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.hb_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("受治疗加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.ihb_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("元素充能效率")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.er_r = Double.parseDouble(value.replace("%", "")) / 100 - role.er_b;
                }
                if (key.contains("冷却缩减")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.cdr_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("护盾强效")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.ss_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("火元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.hsjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("水元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.ssjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("草元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.csjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("雷元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.lsjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("风元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.fsjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("冰元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.bsjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("岩元素伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.ysjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
                if (key.contains("物理伤害加成")) {
                    String value = hashMap.get(key).replace(" ", "").replace(",", "");
                    role.wsjc_r = Double.parseDouble(value.replace("%", "")) / 100;
                }
            } catch (NumberFormatException e) {
                continue;
            }
        }

        role.RefreshTotal();
        System.out.println(role);

        return Result.success("OCR成功",role);
    }

    /**
     * 提交计算逻辑
     *
     * @param role 前端传递而来的角色
     * @return统一返回类型
     */
    @Override
    public Result SubmitForCalculation(Role role) {

        /*
            使用 CalculationUtil 工具类区分角色
            调用 CalculationUtil.distinguishingRoles(role) 方法返回一个map
        */
        CalculationUtil calculationUtil = new CalculationUtil();
        LinkedHashMap<String, Double> linkedHashMap = calculationUtil.distinguishingRoles(role);
        return Result.success(linkedHashMap);
    }


}
